package com.example.admin.testdemo.rs232;

import android.util.Log;

import com.example.admin.testdemo.util.CRCUtil;

/**
 * Created by apple on 2017/12/2.
 */

public class Command {

    public static final byte REQUEST_HEAD = (byte) 0x25;
    public static final byte RESPONSE_HEAD = (byte) 0xAA;
    public static final byte TAIL = (byte) 0x55;

    public static int random = 0x00;


    public static int getRandomId() {
        if (0xFF == random) {
            return random = 0;
        } else {
            return random += 1;
        }
    }

    //连机握手 判断是否有马达控制板
    public static byte[] handshake(int address) {
        byte[] commands = new byte[9];
        commands[0] = 0x25;
        commands[1] = (byte) address;
        commands[2] = (byte) 0xF1;
        commands[3] = (byte) 0x48;
        commands[4] = (byte) 0x69;
        commands[5] = (byte) 0x3F;
        commands[6] = 0x55;
        commands[7] = (byte) getRandomId();
        commands[8] = checksum(commands);
        Log.e("DDD", "发送："+Command.printByteInfo(commands));
        return commands;
    }

    //查询主板工作状态
    public static byte[] mainboardState() {
        byte[] commands = new byte[9];
        commands[0] = (byte)0x25;
        commands[1] = (byte) 0x01;
        commands[2] = (byte) 0xF2;
        commands[3] = (byte) 0x4F;
        commands[4] = (byte) 0x4B;
        commands[5] = (byte) 0x3F;
        commands[6] = (byte)0x55;        ;
        commands[7] = (byte) getRandomId();
        commands[8] = checksum(commands);
        return commands;
    }

    //查询掉货红外检测结果
    public static byte[] outGoodsResult() {
        byte[] commands = new byte[9];
        commands[0] = 0x25;
        commands[1] = (byte) 0x01;
        commands[2] = (byte) 0xF3;
        commands[3] = (byte) 0x52;
        commands[4] = (byte) 0x46;
        commands[5] = (byte) 0x3F;
        commands[6] = 0x55;
        commands[7] = (byte) getRandomId();
        commands[8] = checksum(commands);
        return commands;
    }

    //电机转动(出货)
    //表示哪一层 0x0A~0x0F 表示1到6层
    //表示哪一货道0x00~0x09   表示1到10货道
    public static byte[] outGoods(int address,int level, int road) {
        byte[] commands = new byte[9];
        commands[0] = 0x25;
        commands[1] = (byte) address;
        commands[2] = (byte) 0xF4;
        commands[3] = (byte) (level+9);
        commands[4] = (byte) (road-1);
        commands[5] = (byte) 0xEE;
        commands[6] = 0x55;
        commands[7] = (byte) getRandomId();
        commands[8] = checksum(commands);
        Log.e("TableOneFragment", "发送："+Command.printByteInfo(commands));
        return commands;
    }

    //查询电机转动结果
    public static byte[] engineResult(int lay) {
        byte[] commands = new byte[9];
        commands[0] = 0x25;
        commands[1] = (byte) lay;
        commands[2] = (byte) 0xF5;
        commands[3] = (byte) 0x52;
        commands[4] = (byte) 0x75;
        commands[5] = (byte) 0x6E;
        commands[6] = 0x55;
        commands[7] = (byte) getRandomId();
        commands[8] = checksum(commands);
        return commands;
    }

    //返回瞬时电机电流值
    public static byte[] electrical() {
        byte[] commands = new byte[9];
        commands[0] = 0x25;
        commands[1] = (byte) 0x01;
        commands[2] = (byte) 0xF6;
        commands[3] = (byte) 0x43;
        commands[4] = (byte) 0x75;
        commands[5] = (byte) 0x72;
        commands[6] = 0x55;
        commands[7] = (byte) getRandomId();
        commands[8] = checksum(commands);
        return commands;
    }

    //返回瞬时电机电压值
    public static byte[] voltage() {
        byte[] commands = new byte[9];
        commands[0] = 0x25;
        commands[1] = (byte) 0x01;
        commands[2] = (byte) 0xF7;
        commands[3] = (byte) 0x56;
        commands[4] = (byte) 0x6F;
        commands[5] = (byte) 0x6C;
        commands[6] = 0x55;
        commands[7] = (byte) getRandomId();
        commands[8] = checksum(commands);
        return commands;
    }

    //返回冷冻柜温度值
    public static byte[] temperature() {
        byte[] commands = new byte[9];
        commands[0] = 0x25;
        commands[1] = (byte) 0x01;
        commands[2] = (byte) 0xF8;
        commands[3] = (byte) 0x54;
        commands[4] = (byte) 0x65;
        commands[5] = (byte) 0x6D;
        commands[6] = 0x55;
        commands[7] = (byte) getRandomId();
        commands[8] = checksum(commands);
        return commands;
    }

    //冷冻柜压缩机输出
    //0x30 开   0x31 关
    public static byte[] compressor(int onoff) {
        byte[] commands = new byte[9];
        commands[0] = 0x25;
        commands[1] = (byte) 0x01;
        commands[2] = (byte) 0xF9;
        commands[3] = (byte) 0x52;
        commands[4] = (byte) 0x31;
        commands[5] = (byte) 0x30;
        commands[6] = 0x55;
        commands[7] = (byte) getRandomId();
        commands[8] = checksum(commands);
        return commands;
    }

    // LED照明灯输出
    //  0x30 开   0x31 关
    public static byte[] LED(int onoff) {
        byte[] commands = new byte[9];
        commands[0] = 0x25;
        commands[1] = (byte) 0x01;
        commands[2] = (byte) 0xFA;
        commands[3] = (byte) 0x52;
        commands[4] = (byte) 0x32;
        commands[5] = (byte) 0x30;
        commands[6] = 0x55;
        commands[7] = (byte) getRandomId();
        commands[8] = checksum(commands);
        return commands;
    }

    public static byte[] Door() {
        byte[] commands = new byte[9];
        commands[0] = 0x25;
        commands[1] = (byte) 0x01;
        commands[2] = (byte) 0xFB;
        commands[3] = (byte) 0x44;
        commands[4] = (byte) 0x6F;
        commands[5] = (byte) 0x6F;
        commands[6] = 0x55;
        commands[7] = (byte) getRandomId();
        commands[8] = checksum(commands);
        return commands;
    }
    //格子柜
    public static byte[] express(int level, int road) {
        byte[] commands = new byte[9];
        commands[0] = 0x15;
        commands[1] = (byte) 0x01;
        commands[2] = (byte) 0xC3;
        commands[3] = (byte) level;
        commands[4] = (byte) road;
        commands[5] = (byte) 0x11;
        commands[6] = 0x66;
        commands[7] = (byte) getRandomId();
        commands[8] = checksum(commands);
        return commands;
    }
    public static byte checksum(byte[] command) {
        byte temp = (byte) (command[0] ^ command[1]);
        for (int i = 2; i < 9; i++) {

            temp = (byte) (temp ^ command[i]);
        }
        return temp;
    }
    public static  void printBytes(byte [] buffer){
        StringBuffer sb = new StringBuffer();
        for(int i = 0;i<buffer.length;i++){
            sb.append(String.format("%02X ",buffer[i]));
        }
        Log.i("Command",sb.toString());
    }
    public static  String printByteInfo(byte[] buffer){
        StringBuffer sb = new StringBuffer();
        for(int i = 0;i<buffer.length;i++){
            sb.append(String.format("%02X ",buffer[i]));
            sb.append(" ");
        }
       return sb.toString();
    }
    public static  String printByteInfo(byte[] buffer,int length){
        StringBuffer sb = new StringBuffer();
        for(int i = 0;i<length;i++){
            sb.append(String.format("%02X ",buffer[i]));
            sb.append(" ");
        }
       return sb.toString();
    }
    public static  String  printChar(byte[] buffer){
        StringBuffer sb = new StringBuffer();
        for(int i = 0;i<buffer.length;i++){
            sb.append(String.format("%c ",buffer[i]));
        }
      return sb.toString();
    }

    public static byte[] weight(int add) {
        byte[] commands ={(byte)0x01,(byte)0x03,(byte)0x00,(byte)0x01,(byte)0x00,(byte)0x01,
                (byte)0x01,(byte) 0xD5,(byte)0xCA};
        commands[0] = (byte)add;
        commands[1] = (byte) 0x03;
        commands[2] = (byte) 0x00;
        commands[3] = (byte) 0x01;
        commands[4] = (byte) 0x00;
        commands[5] = (byte) 0x01;
        return commands;
    }

}
